<?php
/**
 * Created by PhpStorm.
 * User: Liu
 * Date: 2022/11/21
 * Time: 11:50
 */

namespace app\admin\controller;

use app\admin\common\Base;
use app\admin\model\Forbidden as ForbiddenModel;

/**
 * 禁止访问IP段控制器
 * Class Forbidden
 * @package app\admin\controller
 */
class Forbidden extends Base {
    public function index($keyword = '',$page = 1,$limit = 10){
        if (is_post()){
            if (!empty($keyword)){
                $where[] = ['start|end','=',ip2long($keyword)];
                $count = db('forbidden')
                    ->where($where)
                    ->count();

                $list = db('forbidden')
                    ->where($where)
                    ->page($page,$limit)
                    ->select();
            } else {
                $count = db('forbidden')
                    ->count();

                $list = db('forbidden')
                    ->page($page,$limit)
                    ->select();
            }

            if (!empty($list)){
                foreach ($list as $k => $v){
                    $list[$k]['start'] = long2ip($v['start']);
                    $list[$k]['end'] = long2ip($v['end']);
                }
            }

            return json(['code' => 'suc','msg' => '数据请求成功','count' => $count,'data' => $list,'current_page'=>$page]);
        }
        return $this->fetch();
    }

    public function add(){
        if (is_post()){
            $model = new ForbiddenModel();
            $model->allowField(true)->save(input('post.'));
            if ($model->id){
                insert_system_log('新增禁止访问IP记录：' . $model->id);
                return ['code' => 'suc', 'msg' => '新增成功'];
            } else {
                return ['code' => 'err', 'msg' => '新增禁止访问IP记录失败，请重试'];
            }
        }
        return $this->fetch();
    }

    public function edit($id){
        if (is_post()){
            $model = new ForbiddenModel();
            $result = $model->allowField(true)->save(input('post.'),['id' => $id]);
            if ($result){
                insert_system_log('修改禁止访问IP记录：' . $id);
                return ['code' => 'suc', 'msg' => '更新成功'];
            } else {
                return ['code' => 'err', 'msg' => '更新失败，请重试'];
            }
        }
        $info = db('forbidden')
            ->where('id',$id)
            ->find();
        $info['start'] = long2ip($info['start']);
        $info['end'] = long2ip($info['end']);
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function del($id){
        if (is_post()){
            $res = db('forbidden')->where('id',$id)->delete();
            if ($res){
                return json(['code' => 'suc','msg' => '已删除']);
            }
            return json(['code' => 'err','msg' => '系统繁忙，请重试']);
        }
    }

    public function upload(){
        $file = request()->file('file');
        $save_path = 'uploads/forbidden/';
        $size = intval(get_system_val('file_size'));
        //0为无限制，手动赋值一个足够大的数值
        if ($size == 0){
            $size = 9999999999 * 10;
        }
        $ext = get_system_val('img_ext');
        $config = [
            'size' => $size,//最大允许上传1M
            'ext' => $ext//允许上传的文件类型
        ];

        $info = $file
            ->validate($config)
            ->move($save_path);

        $ext = $info->getExtension();
        if ($ext == 'xlsx'){
            $reader = \PHPExcel_IOFactory::createReader('excel2007');
        } elseif ($ext == 'xls'){
            $reader = \PHPExcel_IOFactory::createReader('excel5');
        }
        $file_path = $save_path . $info->getFilename();

        $excel = $reader->load("$file_path",$encode = 'utf-8');
        //读取第一张表
        $sheet = $excel->getsheet(0);
        //获取总行数
        $row_num = $sheet->getHighestRow();

        $excel_data = [];
        for ($i = 2; $i <= $row_num; $i++) {
            $excel_data[$i]['start'] = $sheet->getCell('A'.$i)->getValue();
            $excel_data[$i]['end'] = $sheet->getCell('B'.$i)->getValue();
        }

        $model = new ForbiddenModel();
        $res = $model->saveAll($excel_data,false);
        if ($res){
            return json(['code' => 'suc','msg' => '导入成功']);
        }
        return json(['code' => 'err','msg'=>'导入失败，请重试']);
    }
}